import { ref, onMounted } from "vue";
interface IPage {
  limit: number;
  page: number;
}
interface IForm {
  pageNum: number;
  pageSize: number;
}
export default function pageEvent(form: IForm, api: (n: any) => any) {
  let tableData = ref([]);
  let total = ref(0);
  const getList = async () => {
    const res = await api(form);
    tableData.value = res.records.map((item: any, index: number) => {
      item.id = index + 1;
      return item;
    });
    total.value = res.total;
  };
  onMounted(() => {
    getList();
  });
  async function handleCurrentChange(data: IPage) {
    form.pageNum = data.page;
    getList();
  }
  async function handleSizeChange(data: IPage) {
    form.pageNum = 1;
    form.pageSize = data.limit;
    getList();
  }
  return {
    handleCurrentChange,
    handleSizeChange,
    tableData,
    total,
    getList,
  };
}
